﻿/*
 * @author: wizardc
 */

using System;
using System.Collections.Generic;
using System.IO;
using Dou.Resource;
using DouEditor.Resource.ABBuild;
using UnityEditor;

namespace DouEditor.Resource.Common
{
    /// <summary>
    /// 资源同名文件检测
    /// </summary>
    [InitializeOnLoad]
    public class CheckResShortName
    {
        static CheckResShortName()
        {
            AssetDatabaseLoader.getEditorUsedShortName = GetEditorUsedShortName;
        }
        
        /// <summary>
        /// 获取编辑器使用的简短文件名
        /// </summary>
        public static bool GetEditorUsedShortName(out string error, out Dictionary<string, string> short2Full)
        {
            var editorUseBuildRule = BuildSetting.LoadSetting().editorUseBuildRule;
            if (string.IsNullOrWhiteSpace(editorUseBuildRule) || !File.Exists(editorUseBuildRule))
            {
                error = "没有选择编辑器使用的打包规则！";
                short2Full = new Dictionary<string, string>();
                return false;
            }
            var buildRule = AssetDatabase.LoadAssetAtPath<BuildRule>(editorUseBuildRule);
            var abInfo = new CollectABInfo().Collect(buildRule.rules);
            new BuildRaw().Build(buildRule, abInfo);
            return CheckShortName(abInfo, out error, out short2Full);
        }
        
        /// <summary>
        /// 检测简短文件名的唯一性
        /// </summary>
        public static bool CheckShortName(ABInfo abInfo, out string error, out Dictionary<string, string> short2Full)
        {
            short2Full = new Dictionary<string, string>();
            // AB 包含文件
            foreach (var fullName in abInfo.allFile)
            {
                var abName = abInfo.file2AB[fullName];
                if (abInfo.abWriteType[abName] == ManifestWriteType.NoWrite)
                {
                    continue;
                }
                var shortName = Path.GetFileNameWithoutExtension(fullName);
                shortName = shortName.ToLower(); // 打包后大小写不敏感，所以统一转小写后检查
                if (short2Full.ContainsKey(shortName))
                {
                    error = $"发现同名文件\"{fullName}\"和\"{short2Full[shortName]}\"！";
                    return false;
                }
                short2Full.Add(shortName, fullName);
            }
            // 直接复制的文件
            foreach (var kv in abInfo.rawNames)
            {
                var shortName = kv.Key;
                if (short2Full.ContainsKey(shortName))
                {
                    error = $"发现同名文件\"{kv.Value}\"和\"{short2Full[shortName]}\"！";
                    return false;
                }
                short2Full.Add(shortName, kv.Value);
            }
            error = String.Empty;
            return true;
        }
        
        public bool Check(BuildRule buildRule, out string error)
        {
            var abInfo = new CollectABInfo().Collect(buildRule.rules);
            new BuildRaw().Build(buildRule, abInfo);

            if (!CheckShortName(abInfo, out error, out _))
            {
                return false;
            }

            error = String.Empty;
            return true;
        }
    }
}
